package com.jzb.binmvvmlib.handlecallback;

import android.util.Log;

import com.google.gson.JsonSyntaxException;
import com.jzb.binmvvmlib.R;
import com.jzb.binmvvmlib.http.net.NetWorkUtils;
import com.jzb.binmvvmlib.model.Resource;
import com.jzb.binmvvmlib.util.ToastUtils;


import java.net.ConnectException;
import java.net.SocketTimeoutException;

import static com.jzb.binmvvmlib.BaseApplication.getContext;


/**
 * author jzb
 * Date: 2021/4/9
 * Describe:
 */
public abstract class OnCallBack<T> implements Resource.OnHandleCallback<T> {

    private static final String TAG = "OnCallback";
    @Override
    public void onLoading(String msg) {

    }

    @Override
    public void onError(Throwable throwable) {
        Log.e(TAG, "onError: "+throwable );
        if (!NetWorkUtils.isNetworkConnected(getContext())) {
            ToastUtils.showToast(getContext().getResources().getString(R.string.result_network_error));
            return;
        }

        if (throwable instanceof ConnectException) {
            ToastUtils.showToast(getContext().getResources().getString(R.string.result_server_error));
        } else if (throwable instanceof SocketTimeoutException) {
            ToastUtils.showToast(getContext().getResources().getString(R.string.result_server_timeout));
        } else if (throwable instanceof JsonSyntaxException) {
            ToastUtils.showToast("数据解析出错");
        } else {
            ToastUtils.showToast(getContext().getResources().getString(R.string.result_empty_error));
        }
    }

    @Override
    public void onFailure(String msg) {
        ToastUtils.showToast(msg);
    }

    @Override
    public void onCompleted() {

    }

    @Override
    public void onProgress(int precent, long total) {

    }
}